package com.gitee.wsl.ext.string

fun String.countMatches(sub: String): Int {
    if (isEmpty() || sub.isEmpty()) {
        return 0
    }
    var count = 0
    var idx = 0
    while ((indexOf(sub, idx).also { idx = it }) != -1) {
        count++
        idx += sub.length
    }
    return count
}

internal fun countChar(str: String?, c: Char): Int {
    if (str == null) return 0
    var cnt = 0
    var i = 0
    val n = str.length
    while (i < n) {
        if (str.get(i) == c) cnt++
        i++
    }
    return cnt
}